package com.nanjia.boot.myflow.order.item.controller;

import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.commonservice.CommonServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nanjia.boot.myflow.order.item.model.form.OrderItemForm;
import com.nanjia.boot.myflow.order.item.model.query.OrderItemQuery;
import com.nanjia.boot.myflow.order.item.model.vo.OrderItemVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 订单商品明细前端控制层
 *
 * @author 周洋
 * @since 2025-07-18 14:59
 */
@Tag(name = "订单商品明细接口")
@RestController
@RequestMapping("/api/v1/order-item")
@RequiredArgsConstructor
public class OrderItemController  {

    private final CommonServiceImpl commonMyFlowService;

    @Operation(summary = "订单商品明细分页列表")
    @GetMapping("/page")
    public PageResult<OrderItemVO> getOrderItemPage(OrderItemQuery queryParams ) {
        IPage<OrderItemVO> result = commonMyFlowService.getOrderItemPage(queryParams);
        return PageResult.success(result);
    }
    @Operation(summary = "订单商品明细列表")
    @GetMapping("/list")
    public Result<List<OrderItemVO>> getOrderItemList(OrderItemQuery queryParams) {

        List<OrderItemVO> result = commonMyFlowService.getOrderItemList(queryParams);

        return Result.success(result);
    }

    @Operation(summary = "新增订单商品明细")
    @PostMapping
    public Result<Void> saveOrderItem(@RequestBody @Valid OrderItemForm formData ) {
        boolean result = commonMyFlowService.saveOrderItem(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取订单商品明细表单数据")
    @GetMapping("/{id}/form")
    public Result<OrderItemForm> getOrderItemForm(
        @Parameter(description = "订单商品明细ID") @PathVariable Long id
    ) {
        OrderItemForm formData = commonMyFlowService.getOrderItemFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改订单商品明细")
    @PutMapping(value = "/{id}")
    public Result<Void> updateOrderItem(
            @Parameter(description = "订单商品明细ID") @PathVariable Long id,
            @RequestBody @Validated OrderItemForm formData
    ) {
        boolean result = commonMyFlowService.updateOrderItem(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除订单商品明细")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteOrderItems(
        @Parameter(description = "订单商品明细ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = commonMyFlowService.deleteOrderItems(ids);
        return Result.judge(result);
    }

    @Operation(summary = "加入购物车")
    @PostMapping(value = "/addItemToCart")
   //@PreAuthorize(@ss.hasPerm('order:order:query')")
    public Result<Void> addItemToCart(@RequestBody @Valid OrderItemForm formData, HttpServletRequest request){
        boolean result = commonMyFlowService.addItemToCart(formData, IPUtils.getIpAddr(request));
        if (result) {
            return Result.success();
        } else {
            return Result.failed("加入购物车失败");
        }

    }

    @Operation(summary = "加入购物车")
    @PostMapping(value = "/addItemToCartByOrderId")
    //@PreAuthorize(@ss.hasPerm('order:order:query')")
    public Result<Void> addItemToCartByOrderId(@RequestBody @Valid OrderItemForm formData, HttpServletRequest request){
        boolean result = commonMyFlowService.addItemToCart(formData, IPUtils.getIpAddr(request));
        if (result) {
            return Result.success();
        } else {
            return Result.failed("加入购物车失败");
        }
    }
}
